package edu.ustb.efws.base.controller;

import edu.ustb.efws.base.controller.form.ReasonSaveForm;
import edu.ustb.efws.base.controller.form.ReasonUpdateForm;
import edu.ustb.efws.base.controller.vo.QueryRiskReasonVo;
import edu.ustb.efws.base.domain.Reason;
import edu.ustb.efws.base.service.ReasonService;
import edu.ustb.efws.controller.vo.OkVo;
import edu.ustb.efws.controller.vo.ResultPageVo;
import edu.ustb.efws.controller.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 风险原因API接口
 *
 * @author TS Group
 */
@RestController
@RequestMapping("/manage/reason")
@Api(value = "/manage/reason", tags = "风险原因管理")
public class ReasonController {
    private final ReasonService service;

    @Autowired
    public ReasonController(ReasonService service) {
        this.service = service;
    }

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("新增风险原因")
    public ResultVo<Reason> save(@Valid @RequestBody ReasonSaveForm form){
        return ResultVo.success(service.save(form.toDomain()));
    }

    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("修改风险原因")
    public ResultVo<Reason> update(@Valid @RequestBody ReasonUpdateForm form){
        return ResultVo.success(service.update(form.toDomain()));
    }

    @DeleteMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("删除风险原因")
    public ResultVo<OkVo> delete(@PathVariable("id")int id){
        return ResultVo.success(new OkVo(service.delete(id)));
    }

    @GetMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取风险原因")
    public ResultVo<Reason> get(@PathVariable("id")int id){
        return ResultVo.success(service.get(id));
    }

    @GetMapping(value = "riskReason", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("查询危险原因")
    public ResultVo<QueryRiskReasonVo> queryRisk(@RequestParam(value = "riskType")Integer riskType,
                                                 @RequestParam(value = "score")Float score){

        List<Reason> reasonObjs = service.queryRisk(riskType, score);

        if(reasonObjs.isEmpty()){
            return ResultVo.success(new QueryRiskReasonVo(new String[0], new String[0]));
        }

        List<String> reasons = new ArrayList<>();
        List<String> measures = new ArrayList<>();

        for(Reason reason: reasonObjs){
            reasons.addAll(Arrays.asList(StringUtils.split(reason.getReason(), ",")));
            measures.addAll(Arrays.asList(StringUtils.split(reason.getMeasure(), ",")));
        }

        return ResultVo.success(new QueryRiskReasonVo(reasons.toArray(new String[0]), measures.toArray(new String[0])));
    }

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("查询风险原因")
    public ResultPageVo<Reason> query(
            @ApiParam(value = "类型;0代表电流，1代表温度，2代表湿度") @RequestParam(required = false) Integer type,
            @ApiParam(value = "情形id") @RequestParam(required = false) Integer caseId,
            @ApiParam(value = "情形描述") @RequestParam(required = false) String caseInfo,
            @RequestParam(defaultValue = "true") @ApiParam(value = "是否查询记录数") Boolean isCount,
            @RequestParam(defaultValue = "0") @ApiParam(value = "查询页数") int page,
            @RequestParam(defaultValue = "15") @ApiParam(value = "查询每页记录数") int rows){

        return new ResultPageVo
                .Builder<>(page, rows, service.query(type, caseId, caseInfo, page * rows, rows))
                .count(isCount, () -> service.count(type, caseId, caseInfo))
                .build();
    }
}
